﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;
using Sector7.Medical.Consts;
using Sector7.Medical.Biz;
using Sector7.Medical.EntityType;

namespace Sector7.Medical.STOM.Windows
{
    /// <summary>
    /// AddVendorWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddVendorWindow : AddModeWindowBase
    {
        #region Accessor
        /// <summary>
        /// 获取或设置窗口关联供应商信息
        /// </summary>
        public M7_Vendor Vendor
        {
            get { return m_entity as M7_Vendor; }
            set { m_entity = value; }
        }
        #endregion

        #region Public functions
        #endregion

        #region Private functions
        protected override void InitializeForCreating()
        {
            txbName.Text = string.Empty;
            txbContact.Text = string.Empty;
            txbTelephone.Text = string.Empty;
            txbAddress.Text = string.Empty;
            txbRemarks.Text = string.Empty;

            m_entity = new M7_Vendor();

            base.InitializeForCreating();
        }

        protected override void InitializeForUpdating()
        {
            txbId.Text = Vendor.VendorId.ToString();
            txbName.Text = Vendor.VendorName;
            txbContact.Text = Vendor.ContacterId;
            txbTelephone.Text = Vendor.VendorPhone;
            txbAddress.Text = Vendor.VendorAddress;
            txbRemarks.Text = Vendor.Remarks;

            base.InitializeForUpdating();
        }

        protected override bool CheckData()
        {
            m_errors.Clear();

            Vendor.VendorName = txbName.Text;
            Vendor.ContacterId = txbContact.Text;
            Vendor.VendorPhone = txbTelephone.Text;
            Vendor.VendorAddress = txbAddress.Text;
            if (string.IsNullOrWhiteSpace(Vendor.VendorName))
            {
                m_errors.Add("供货商名称", "不能为空！");
            }
            if (string.IsNullOrWhiteSpace(Vendor.ContacterId))
            {
                m_errors.Add("联系人", "不能为空！");
            }
            if (string.IsNullOrWhiteSpace(Vendor.VendorPhone))
            {
                m_errors.Add("供货商电话", "不能为空！");
            }
            if (string.IsNullOrWhiteSpace(Vendor.VendorAddress))
            {
                m_errors.Add("供货商地址", "不能为空！");
            }

            return base.CheckData();
        }

        protected override void AddData()
        {
            Vendor.Remarks = txbRemarks.Text;

            int result;
            switch (m_crud)
            {
                case CrudType.Create:
                    result = (int)Agent_INFS.AddVendor(Vendor);
                    if (result > 0)
                    {
                        Vendor.VendorId = result;
                    }
                    break;
                case CrudType.Update:
                    bool isChanged = false;
                    EntityDeltaBase delta = GetDelta(C_PrimaryKeyName.VENDOR_ID, ref isChanged);

                    if (isChanged)
                    {
                        result = Agent_INFS.SetVendor(delta);
                    }
                    break;
                default:
                    break;
            }

            base.AddData();
        }
        #endregion

        #region Structure
        /// <summary>
        /// 创建添加供应商窗口对象
        /// </summary>
        public AddVendorWindow()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        #endregion
    }
}
